//Team 4 - Gold Coast
//Duy Kha Nguyen 598240 
//Rongkai Zhou 562946 
//Zhangyue Cheng 548692
//Binzan Liang 591911
//Wei Liu 572322
package couchdb;

import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.lightcouch.CouchDbClient;

public class ExportTweetTask implements Runnable {

    private static final Logger logger = Logger.getLogger(ExportTweetTask.class);

    private final CouchDbClient dataSource;

    private final BlockingQueue<Map> outputQueue;

    public ExportTweetTask(CouchDbClient dataSource, BlockingQueue<Map> outputQueue) {
        super();
        this.dataSource = dataSource;
        this.outputQueue = outputQueue;
    }

    public void run() {
        try {
            dataSource.syncDesignDocsWithDb();
            final List<Map> allTweets = dataSource.view("tweet/AllTweets").includeDocs(false).query(Map.class);
            for (final Map tweet : allTweets) {
                outputQueue.put((Map) tweet.get("value"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Unexpected exception", e);
        }

    }
}
